package com.yida.gwj.common.utils;

import com.yida.common.core.text.Convert;
import com.yida.common.core.utils.StringUtils;
import com.yida.gwj.common.utils.jexl.GwjCalculator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.math.BigDecimal;
import java.util.*;

/***
 * @author lqq
 **/
public class GwjCalculateFormulaUtil {

	private static final Logger log = LoggerFactory.getLogger(GwjCalculateFormulaUtil.class);

	/**
	 * 根据key替换参数形成公式
	 *
	 * @param formula 公式
	 * @param data    请求参数
	 * @return /
	 */
	public static BigDecimal getFormulaResult(String formula, Map<String, Object> data) {
		String[] f = StringUtils.split(formula, "_");
		StringBuilder r = new StringBuilder();
		for (String s : f) {
			if (StringUtils.isNotNull(data.get(s))) {
				r.append(Convert.toStr(data.get(s)));
			} else {
				r.append(s);
			}
		}
		System.out.println(r.toString());
		return GwjCalculator.conversion(r.toString());
	}

	public static void main(String[] args) {
		Map<String, Object> map = new HashMap<>(16);
		map.put("totalMoney", 10);
		map.put("totalSize", 3);
		map.put("depositSize", 2);
		System.out.println(getFormulaResult("totalMoney_/_totalSize_*_depositSize", map));
	}
}
